package com.itheima.project.face.impl;

import com.itheima.project.pojo.ContentRuleDetail;
import com.itheima.project.service.IContentRuleDetailService;
import com.itheima.project.vo.content.ContentRuleDetailVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.project.utils.BeanConv;
import com.itheima.project.face.ContentRuleDetailFace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import com.itheima.project.utils.ExceptionsUtil;
import com.itheima.project.exception.ProjectException;
import com.itheima.project.constant.content.ContentRuleDetailCacheConstant;
import com.itheima.project.core.enums.content.ContentRuleDetailEnum;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
 * @Description：Face服务实现类
 */
@Slf4j
@Component
public class ContentRuleDetailFaceImpl implements ContentRuleDetailFace {

    @Autowired
    IContentRuleDetailService contentRuleDetailService;


    @Override
    @Cacheable(value = ContentRuleDetailCacheConstant.PAGE,key ="#pageNum+'-'+#pageSize+'-'+#contentRuleDetailVo.hashCode()")
    public Page<ContentRuleDetailVo> findContentRuleDetailVoPage(ContentRuleDetailVo contentRuleDetailVo, int pageNum, int pageSize) {
        try {
            Page<ContentRuleDetail> page = contentRuleDetailService.findContentRuleDetailPage(contentRuleDetailVo, pageNum, pageSize);
            Page<ContentRuleDetailVo> pageVo =BeanConv.toPage(page,ContentRuleDetailVo.class);
            return pageVo;
        }catch (Exception e){
            log.error("列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ContentRuleDetailEnum.PAGE_FAIL);
        }
    }

    @Transactional
    @Caching(evict = {@CacheEvict(value = ContentRuleDetailCacheConstant.PAGE,allEntries = true),
        @CacheEvict(value = ContentRuleDetailCacheConstant.LIST,allEntries = true)},
    put={@CachePut(value =ContentRuleDetailCacheConstant.BASIC,key = "#result.id")})
    @Override
    public ContentRuleDetailVo createContentRuleDetailVo(ContentRuleDetailVo contentRuleDetailVo) {
        try {
            ContentRuleDetailVo contentRuleDetailVoResult = BeanConv.toBean(contentRuleDetailService.createContentRuleDetail(contentRuleDetailVo), ContentRuleDetailVo.class);
            return contentRuleDetailVoResult;
        } catch (Exception e) {
            log.error("保存异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ContentRuleDetailEnum.SAVE_FAIL);
        }
    }

    @Transactional
    @Caching(evict = {@CacheEvict(value = ContentRuleDetailCacheConstant.PAGE,allEntries = true),
        @CacheEvict(value = ContentRuleDetailCacheConstant.LIST,allEntries = true),
        @CacheEvict(value =ContentRuleDetailCacheConstant.BASIC,key = "#contentRuleDetailVo.id")})
    @Override
    public Boolean updateContentRuleDetailVo(ContentRuleDetailVo contentRuleDetailVo) {
        try {
            Boolean flag = contentRuleDetailService.updateContentRuleDetail(contentRuleDetailVo);
            return flag;
        } catch (Exception e) {
            log.error("修改异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ContentRuleDetailEnum.UPDATE_FAIL);
        }
    }

    @Transactional
    @Caching(evict = {@CacheEvict(value = ContentRuleDetailCacheConstant.PAGE,allEntries = true),
        @CacheEvict(value = ContentRuleDetailCacheConstant.LIST,allEntries = true)})
    @Override
    public Boolean deleteContentRuleDetailVo(String[] checkedIds) {
        try {
            Boolean flag = contentRuleDetailService.deleteContentRuleDetail(checkedIds);
            return flag;
        } catch (Exception e) {
            log.error("删除异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ContentRuleDetailEnum.DEL_FAIL);
        }
    }

    @Override
    @Cacheable(value = ContentRuleDetailCacheConstant.LIST,key ="#contentRuleDetailVo.hashCode()")
    public List<ContentRuleDetailVo> findContentRuleDetailVoList(ContentRuleDetailVo contentRuleDetailVo) {
        try {
            List<ContentRuleDetailVo> records = BeanConv.toBeanList(contentRuleDetailService.findContentRuleDetailList(contentRuleDetailVo),ContentRuleDetailVo.class);
            return records;
        } catch (Exception e) {
            log.error("删除异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ContentRuleDetailEnum.LIST_FAIL);
        }
    }
}
